﻿using System.Collections.Generic;

namespace UFrame
{
    public class SearchCollection<T>
    {
        private Dictionary<int, ItemFun<T>> mExpressionDic = new Dictionary<int, ItemFun<T>>();


        public void Init()
        {
            mExpressionDic.Clear();
        }

        public void AddFilter(int id, ItemFun<T> fun)
        {
            mExpressionDic.Add(id, fun);
        }

        public List<T> Get(Dictionary<int, object> condition, IEnumerable<T> itemCollection)
        {
            List<T> tempList = new List<T>();
            ItemFun<T> func = null;
            bool isNotThrough = false;
            foreach (var item in itemCollection)
            {
                isNotThrough = true;
                foreach (var cond in condition)
                {
                    func = mExpressionDic[cond.Key];

                    if (func.IsSuit(item, cond.Value) == false)
                    {
                        isNotThrough = false;
                        break;
                    }
                }

                if (isNotThrough)
                {
                    tempList.Add(item);
                }
            }

            return tempList;
        }
    }

    public abstract class ItemFun<T>
    {
        public abstract bool IsSuit(T item, object args);
    }
}